/************************************************************************
 * Operations for users
 *
 * @return - invocationResult
 */

/* Statement for User operation*/		
var authenticateStatement = WL.Server.createSQLStatement("select * from Users where login_id=? and password=?");

function authenticateUser( login_id, password ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : authenticateStatement,
		parameters : [login_id, password]
	});
}


/************************************************************************
 * Operations for Goals
 *
 * @return - invocationResult
 */

/* Statement for Goal operation*/		
var getGoalStatement = WL.Server.createSQLStatement("select * from Goals where id =?");
var createGoalStatement = WL.Server.createSQLStatement("insert Goals (owner_id, Goal, action) values (?,?,?)");
var updateGoalStatement = WL.Server.createSQLStatement("update Goals set Goal = ?, action=? where id=?");
var deleteGoalStatement = WL.Server.createSQLStatement("delete from Goals where id=?");
var listGoalsStatement = WL.Server.createSQLStatement("select id, owner_id, date_format(create_date,'%Y') as update_year, date_format(create_date,'%H:%i') as update_time, goal, action from Goals where owner_id =?");

function getGoal( GoalID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : getGoalStatement,
		parameters : [GoalID]
	});
}

function createGoal( ownerID, Goal, action ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : createGoalStatement,
		parameters : [ownerID, Goal, action]
	});
}

function updateGoal( id, goal, action ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : updateGoalStatement,
		parameters : [goal, action, id]
	});
}

function deleteGoal( GoalID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : deleteGoalStatement,
		parameters : [GoalID]
	});
}

function listGoals( ownerID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : listGoalsStatement,
		parameters : [ownerID]
	});
}


/************************************************************************
 * Operations for thoughts
 *
 * @return - invocationResult
 */

/* Statement for Thought operation*/		
var getThoughtStatement = WL.Server.createSQLStatement("select * from Thoughts where id =?");
var createThoughtStatement = WL.Server.createSQLStatement("insert Thoughts (owner_id, Thought, action) values (?,?,?)");
var updateThoughtStatement = WL.Server.createSQLStatement("update Thoughts set Thought =?, action=? where id=?");
var shareThoughtStatement = WL.Server.createSQLStatement("update Thoughts set shared =? where id=?");
var deleteThoughtStatement = WL.Server.createSQLStatement("delete from Thoughts where id=?");
var listThoughtsStatement = WL.Server.createSQLStatement("select id, owner_id, thought, action, date_format(create_date,'%Y-%m-%d') as update_date, date_format(create_date,'%H:%i') as update_time, shared  from Thoughts where owner_id =?");
var listSharedThoughtsStatement = WL.Server.createSQLStatement("select id, owner_id, thought, action, date_format(create_date,'%Y-%m-%d') as update_date, date_format(create_date,'%H:%i') as update_time  from Thoughts where shared =1");

function getThought( ThoughtID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : getThoughtStatement,
		parameters : [ThoughtID]
	});
}

function createThought( ownerID, Thought, action ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : createThoughtStatement,
		parameters : [ownerID, Thought, action]
	});
}

function updateThought( id, thought, action ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : updateThoughtStatement,
		parameters : [thought, action, id]
	});
}

function shareThought( id, shared) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : shareThoughtStatement,
		parameters : [shared, id]
	});
}

function deleteThought( ThoughtID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : deleteThoughtStatement,
		parameters : [ThoughtID]
	});
}

function listThoughts( ownerID ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : listThoughtsStatement,
		parameters : [ownerID]
	});
}

function listSharedThoughts( ) {

	return WL.Server.invokeSQLStatement({
		preparedStatement : listSharedThoughtsStatement,
		parameters : []
	});
}

